package cn.exrick.manager.controller;

import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbEmailTemplate;
import cn.exrick.manager.service.EmailTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailTemplateController {

    @Autowired
    private EmailTemplateService emailTemplateService;

    @PostMapping("/emailTemplate/add")
    public Result<Object> addEmailTemplate(TbEmailTemplate tbEmailTemplate){
        emailTemplateService.addTemplate(tbEmailTemplate);
        return new ResultUtil<Object>().setData(null);
    }

    @PostMapping("/emailTemplate/update")
    public Result<Object> updateEmailTemplate(TbEmailTemplate tbEmailTemplate){
        emailTemplateService.updateTemplate(tbEmailTemplate);
        return new ResultUtil<Object>().setData(null);
    }


    @PostMapping("/emailTemplate/{type}")
    public Result<Object> updateEmailTemplate(@PathVariable Integer type){
        TbEmailTemplate template = emailTemplateService.getTemplateByType(type);
        return new ResultUtil<Object>().setData(template);
    }


    @PostMapping("/emailTemplate/updateBatch")
    public Result<Object> updateBatch(Integer itemTemplateId,String itemTemplate,Integer orderTemplateId,String orderTemplate){
        TbEmailTemplate itemEmailTemplate = new TbEmailTemplate();
        itemEmailTemplate.setId(itemTemplateId);
        itemEmailTemplate.setTemplate(itemTemplate);
        emailTemplateService.updateTemplate(itemEmailTemplate);

        TbEmailTemplate orderEmailTemplate = new TbEmailTemplate();
        orderEmailTemplate.setId(orderTemplateId);
        orderEmailTemplate.setTemplate(orderTemplate);
        emailTemplateService.updateTemplate(orderEmailTemplate);

        return new ResultUtil<Object>().setData(null);
    }

}
